package com.example.xm.ui.home.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.CommentAdapter;
import com.example.xm.adapter.HomeAdapter.TrendsActivityAdapter;
import com.example.xm.base.BaseActivity;
import com.example.xm.bean.HomeBean.HomeArticleBean;
import com.example.xm.bean.HomeBean.HomeBannderBean;
import com.example.xm.bean.HomeBean.HomeHotUserBean;
import com.example.xm.bean.HomeBean.HomePhotoBean;
import com.example.xm.bean.HomeBean.HomeRecommendBean;
import com.example.xm.bean.HomeBean.HomeRewardBean;
import com.example.xm.bean.HomeBean.HomeSquareBean;
import com.example.xm.bean.HomeBean.HomeTopicDiscussedBean;
import com.example.xm.bean.HomeBean.HomeVideoBean;
import com.example.xm.common.ConStants;
import com.example.xm.customizing.CustomAvaterView;
import com.example.xm.interfaces.home.IHome;
import com.example.xm.presenter.home.RecommendPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecommendDetailActivity extends BaseActivity<IHome.RecommendPersenter> implements IHome.RecommendView {
    @BindView(R.id.recycler_commend)
    RecyclerView recyclerCommend;
    @BindView(R.id.coustomAvaterBox)
    CustomAvaterView coustomAvaterBox;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.rlv_comment)
    RecyclerView rlvComment;
    @BindView(R.id.iv_finish)
    ImageView ivFinish;
    private ArrayList<HomeRecommendBean.DataBean.PostDetailBean> recommend;
    private TrendsActivityAdapter recommendAdapter;
    private static final int MAX_HEADER_NUMBER = 10;
    private ArrayList<HomeRecommendBean.DataBean.CommentsBean.AllCommentsBean> allCommentsBeans;
    private CommentAdapter commentAdapter;

    @Override
    protected IHome.RecommendPersenter initPresenter() {
        return new RecommendPresenter();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_recommend_detail;
    }

    @Override
    protected void initView() {
        setRecommend();
        setCoustomAvaterBox();
        setRlv();
    }

    private void setRlv() {
        allCommentsBeans = new ArrayList<>();
        rlvComment.setLayoutManager(new LinearLayoutManager(this));
        rlvComment.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        commentAdapter = new CommentAdapter(this, allCommentsBeans);
        rlvComment.setAdapter(commentAdapter);
        ArrayList<HomeRecommendBean.DataBean.CommentsBean.AllCommentsBean> bean1 = ConStants.pallComments;
        //添加评论数据
        allCommentsBeans.addAll(bean1);
        commentAdapter.notifyDataSetChanged();
    }

    private void setCoustomAvaterBox() {
        coustomAvaterBox.setMaxCount(MAX_HEADER_NUMBER);
        HomeRecommendBean.DataBean.PostDetailBean bean = ConStants.curClickPost;

        //添加官方小袍，数据
        recommend.add(bean);
        recommendAdapter.notifyDataSetChanged();

        int count = ConStants.countNum;
        Log.i("bean", bean.getContent());
        if (bean != null && bean.getLikeDetails().size() > 0) {
            List<String> headers = new ArrayList<>();
            for (int i = 0; i < bean.getImages().size(); i++) {
                if (i >= MAX_HEADER_NUMBER) break;
                headers.add(bean.getLikeDetails().get(i).getHeadUrl());
            }
            coustomAvaterBox.initDatas(headers);
        }
        tvNum.setText(count + "人点赞》");
    }


    private void setRecommend() {
        recommend = new ArrayList<>();
        recommendAdapter = new TrendsActivityAdapter(this, recommend);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerCommend.setLayoutManager(manager);
        recyclerCommend.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        recyclerCommend.setAdapter(recommendAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getBanner();
        mPresenter.getHotUser();
        mPresenter.getRecommend();
        mPresenter.getTopicDiscussed();
    }

    @Override
    protected void initListener() {
        ivFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void getBannerReturn(HomeBannderBean result) {

    }

    @Override
    public void getTopicDiscussedReturn(HomeTopicDiscussedBean result) {

    }

    @Override
    public void getRecommendReturn(HomeRecommendBean result) {

    }

    @Override
    public void getHotUserReturn(HomeHotUserBean result) {

    }

    @Override
    public void getSquareReturn(HomeSquareBean result) {

    }

    @Override
    public void getVideoReturn(HomeVideoBean result) {

    }

    @Override
    public void getPhotoReturn(HomePhotoBean result) {

    }

    @Override
    public void getHomeArticleReturn(HomeArticleBean result) {

    }

    @Override
    public void getHomeRewardReturn(HomeRewardBean result) {

    }


    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}